from django import forms

class CommentForm(forms.Form):
    """
    评论表单
    """
    # article = forms.CharField(widget=forms.HiddenInput())
    username = forms.CharField(widget=forms.TextInput(attrs={"id": "nickname",
                                                           "class": "form-control",
                                                           "required": "required",
                                                           "size": "25", "tabindex": "1"}))

    email = forms.EmailField(widget=forms.TextInput(attrs={"id":"email", "type":"email",
                                                           "name":"email", "size":"30",
                                                           "class": "form-control",
                                                           "required":"required",
                                                           "placeholder":"邮箱",
                                                            "tabindex":"2"}),
                             max_length=50, error_messages={"required":"email不能为空",})
    url = forms.URLField(widget=forms.TextInput(attrs={"id":"url", "type":"url",
                                                       "name":"url", "class":"form-control",
                                                       "placeholder": "网站",
                                                       "size":"30", "tabindex":"3"}),
                         max_length=100, required=False)

    comment = forms.CharField(widget=forms.Textarea(attrs={"id":"comment","class": "form-control",
                                                           "required": "required", "cols": "25",
                                                           "rows": "5", "tabindex": "4"}),
                              error_messages={"required":"评论不能为空",})
import re
from django.core.exceptions import ValidationError
def mobile_validate(value):
    mobile_re = re.compile(r'^\d{11}$')
    if not mobile_re.match(value):
        raise ValidationError("手机号码格式错误")


class UserInfo(forms.Form):
    email = forms.EmailField(error_messages={"required": "邮箱不能为空"}, label = "邮箱")
    mobile = forms.CharField(validators=[mobile_validate,])
    user_type_choice = ((0, "普通"), (1, "超级"))
    use_type = forms.IntegerField(widget=forms.widgets.Select(choices=user_type_choice))

    def clean_email(self):
        email = self.cleaned_data.get('email')
        return email+'QQ'
